package com.MSB.LinkedList;

public class LinkedListTest {

    /**
     * 反转链表
     * @param head
     * @return
     */
    public static Node reverseLinkedList(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    /**
     * 删除链表中，值为value的节点
     * @param head 头节点
     * @param value 值
     * @return 链表的头节点
     */
    public static Node removeValue(Node head, int value){
        while(head != null){
            if (head.value != value)
                break;
            head = head.next;
        }
        Node pre = head;
        Node cur = head;
        while(cur != null){
            if (cur.value == value){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    /**
     * head
     * a -> b -> c -> null
     * c -> b -> a -> null
     */
    static class Node{
        int value;
        Node next;

        public Node() {
        }

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
}
